package com.eleadmin.common.system.controller;

import cn.hutool.core.date.DateUtil;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.common.system.entity.TProduct;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.common.system.param.TProductParam;
import com.eleadmin.common.system.service.TProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 产品信息控制器
 */
@Api(tags = "产品信息管理")
@RestController
@RequestMapping("/system/product")
public class TProductController extends BaseController {
    @Resource
    private TProductService tProductService;

    @OperationLog
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ApiResult<PageResult<TProduct>> page(TProductParam param) {
        PageParam<TProduct, TProductParam> page = new PageParam<>(param);
        return success(tProductService.page(page, page.getWrapper()));
    }

    @OperationLog
    @ApiOperation("查询全部")
    @GetMapping()
    public ApiResult<List<TProduct>> list(TProductParam param) {
        PageParam<TProduct, TProductParam> page = new PageParam<>(param);
        return success(tProductService.list(page.getOrderWrapper()));
    }

    @OperationLog
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public ApiResult<TProduct> get(@PathVariable("id") Integer id) {
        return success(tProductService.getById(id));
    }

    @OperationLog
    @ApiOperation("添加")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TProduct product) {
        User user = getLoginUser();
        product.setCreateUser(user.getNickname());
        product.setCreateTime(DateUtil.now());
        product.setStatus(0);
        if (tProductService.save(product)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @OperationLog
    @ApiOperation("修改")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TProduct product) {
        if (tProductService.updateById(product)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @ApiOperation("上架下架")
    @PutMapping("/onoff/{id}")
    public ApiResult<?> onoff(@PathVariable("id") Integer id) {
        TProduct product = tProductService.getById(id);
        if(product.getStatus() == 0){
            //下架
            product.setStatus(1);
        } else if(product.getStatus() == 1){
            //上架
            product.setStatus(0);
        }
        if (tProductService.updateById(product)) {
            return success("操作成功");
        }
        return fail("操作失败");
    }

    @OperationLog
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (tProductService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }


}
